//
//  ToolsCustom.m
//  WYModel
//
//  Created by ios on 2017/3/3.
//  Copyright © 2017年 Wy. All rights reserved.
//

#import "ToolsCustom.h"

@implementation ToolsCustom

//手机号
+ (BOOL) validatePhone:(NSString *)phone{
    
    //手机号以13， 15，18，17开头，八个 \d 数字字符
    NSString *CM = @"^1(3[0-9]|5[0-35-9]|8[0-345-9]|7[137])\\d{8}$";    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    return [regextestcm evaluateWithObject:phone];
    //17773956183
}

#pragma mark - 身份证判定
+(BOOL) validateIdentityCard: (NSString *)identityCard
{
    BOOL flag;
    if (identityCard.length <= 0) {
        flag = NO;
        return flag;
    }
    NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
    NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
    return [identityCardPredicate evaluateWithObject:identityCard];
}
#pragma mark -  邮箱正则
+(BOOL)validateEmail:(NSString *)email{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}

#pragma mark -- 正则匹配17位车架号
+ (BOOL) checkCheJiaNumber:(NSString *) CheJiaNumber{
    NSString *bankNum=@"^(\\d{17})$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",bankNum];
    BOOL isMatch = [pred evaluateWithObject:CheJiaNumber];
    return isMatch;
}

#pragma mark - 车牌号判断
+ (BOOL) validateCarNo:(NSString *)carNo
{
    NSString *carRegex = @"^[\u4e00-\u9fa5]{1}[a-zA-Z]{1}[a-zA-Z_0-9]{4}[a-zA-Z_0-9_\u4e00-\u9fa5]$";
    NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",carRegex];
    return [carTest evaluateWithObject:carNo];
}

#pragma mark 护照正则
+(BOOL)validataPassport:(NSString *)passport{
    
    NSString *pass = @"^1[45][0-9]{7}|G[0-9]{8}|P[0-9]{7}|S[0-9]{7,8}|D[0-9]+$";
    NSPredicate *Test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",pass];
    
    return [Test evaluateWithObject:pass];
}

#pragma mark 军官证正则
+(BOOL)validataOfficerID:(NSString *)officerID{
    NSString *pass = @"/[\u4e00-\u9fa5](字第){1}(\d{4,8})(号?)$/";
    NSPredicate *Test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",pass];
    return [Test evaluateWithObject:officerID];
}

#pragma mark 邮编正则
+(BOOL)validataZipCode:(NSString *)code{
    NSString *pass = @"[1-9]\\d{5}(?!\d)";
    NSPredicate *Test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",pass];
    return [Test evaluateWithObject:code];
}

#pragma mark -  转换空字符串
+ (NSString*) convertNull:(id)object
{
    if ([object isEqual:[NSNull null]]) {
        return @"";
    }else if ([object isKindOfClass:[NSNull class]]){
        return @"";
    }else if (object==nil){
        return @"";
    }
    NSString *resultStr = [NSString stringWithFormat:@"%@", object];
    return resultStr;
}

#pragma mark -- 获取机器识别码
+ (NSString*)uniqueAppInstanceIdentifier
{
    NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
    static NSString* UUID_KEY = @"MPUUID";
    NSString* app_uuid = [userDefaults stringForKey:UUID_KEY];
    
    if (app_uuid == nil) {
        
        CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);
        
        CFStringRef uuidString = CFUUIDCreateString(kCFAllocatorDefault, uuidRef);
        
        app_uuid = [NSString stringWithString:(__bridge NSString*)uuidString];
        
        [userDefaults setObject:app_uuid forKey:UUID_KEY];
        
        [userDefaults synchronize];
        
        CFRelease(uuidString);
        
        CFRelease(uuidRef);
        
    }
    return app_uuid;
}

#pragma mark --删除文件夹
+ (void)removeFileName:(NSString *)fileName{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = [paths objectAtIndex:0];
    NSString *imageDir =[documentDirectory stringByAppendingPathComponent:fileName];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    NSError *error;
    if([fileManager removeItemAtPath:imageDir error:&error] == NO){
        //   NSLog(@"Failed to remove path %@, ERROR = %@",imageDir,error);
        //    NSLog(@"删除失败");
    }else{
        //    NSLog(@"删除成功");
    }
    
}

#pragma mark--刷新UItableView某行
+(void)refeshUITableView:(UITableView *)tableView section:(NSInteger )section row:(NSInteger )row{
    
    NSIndexPath  *indexPath_2=[NSIndexPath indexPathForRow:row inSection:section];
    NSArray      *indexArray2=[NSArray  arrayWithObject:indexPath_2];
    [tableView  reloadRowsAtIndexPaths:indexArray2 withRowAnimation:UITableViewRowAnimationNone];
}

#pragma mark - 将字典或者数组转化为JSON串
+ (NSString *)toJSONData:(id)theData{
    
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:theData
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:&error];
    
    if ([jsonData length] > 0 && error == nil){
        NSString *jsonStr = [[NSString alloc] initWithData:jsonData
                                                  encoding:NSUTF8StringEncoding];
        jsonStr = [jsonStr stringByReplacingOccurrencesOfString:@"\n" withString:@""];
        jsonStr = [jsonStr stringByReplacingOccurrencesOfString:@" " withString:@""];
        return jsonStr;
    }else{
        return nil;
    }
}

#pragma mark - 将JSON串转化为字典或者数组
+ (id)toArrayOrNSDictionary:(NSString *)jsonString{
    NSData * jsonData = [jsonString dataUsingEncoding:NSASCIIStringEncoding];
    NSError *error = nil;
    id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData
                                                    options:NSJSONReadingAllowFragments
                                                      error:&error];
    
    if (jsonObject != nil && error == nil){
        return jsonObject;
    }else{
        // 解析错误
        return nil;
    }
}

#pragma mark -时间戳转字符串
+(NSString *)timestampToString:(NSString *)timestamp{
    
    NSTimeInterval time=[[NSString stringWithFormat:@"%@",timestamp] doubleValue];
    NSDate*detaildate=[NSDate dateWithTimeIntervalSince1970:time];
    //实例化一个NSDateFormatter对象
    NSDateFormatter*dateFormatter = [[NSDateFormatter alloc]init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString*currentDateStr = [dateFormatter stringFromDate:detaildate];
    return  currentDateStr;
}

+(NSString *)timestampToString:(NSString *)timestamp withDataFormat:(NSString *)dataFormat{
    
    NSTimeInterval time=[[NSString stringWithFormat:@"%@",timestamp] doubleValue];
    NSDate*detaildate=[NSDate dateWithTimeIntervalSince1970:time];
    //实例化一个NSDateFormatter对象
    NSDateFormatter*dateFormatter = [[NSDateFormatter alloc]init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:dataFormat];
    NSString*currentDateStr = [dateFormatter stringFromDate:detaildate];
    return  currentDateStr;
}




@end
